﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class MarkOperation
	{
		private List<string> GetHYTagNames()
		{
			return new UserSetting().GetTagNameList();
		}

		public void SetActiveDocAllLoadedTags(Autodesk.Revit.DB.Document doc, bool beOnlyFont = false)
		{
			CommonFont fontSetting = new UserSetting().GetFontSetting();
			List<string> hytagNames = this.GetHYTagNames();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(Family));
			try
			{
				IList<Element> list = filteredElementCollector.ToElements();
				if (list != null && list.Count >= 1)
				{
					foreach (Element element in list)
					{
						Family family = element as Family;
						if (family != null && hytagNames.Contains(family.Name))
						{
							this.EditHYFamilyFont(family, fontSetting, beOnlyFont);
						}
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
			}
		}

		public TextNote GetTagTextWidthAndHeight(string tagText, Autodesk.Revit.DB.Document doc, ref double width, ref double height)
		{
			SubTransaction subTransaction = new SubTransaction(doc);
			TextNote textNote = null;
			try
			{
				subTransaction.Start();
				XYZ startPoint = new XYZ(0.0, 0.0, 0.0);
				TextNoteType specialTextNodeType = this.GetSpecialTextNodeType(doc);
				textNote = UnifiedModified.HYCreatTextNote(doc, tagText, startPoint, specialTextNodeType);
				doc.Regenerate();
				double num = (double)doc.ActiveView.Scale * 304.8;
				width = UnifiedModified.GetTextNoteWidth(doc, textNote) * 304.8;
				height = textNote.Height * num * 1.1;
				subTransaction.RollBack();
			}
			catch (Exception ex)
			{
				string message = ex.Message;
                if (subTransaction != null && (int)subTransaction.GetStatus() == 1)
				{
					subTransaction.RollBack();
				}
			}
			return textNote;
		}

		public double GetDistanceTagToPipe(Pipe pipe, string tagText)
		{
			Document document = pipe.Document;
			Autodesk.Revit.DB.View activeView = document.ActiveView;
			CommonFont fontSetting = new UserSetting().GetFontSetting();
			fontSetting.TubeDistance = this.GetTansferDistance(fontSetting.TubeDistance, 30);
			int num = document.ActiveView.Scale / 100;
			double num2 = 0.0;
			double num3 = 0.0;
			this.GetTagTextWidthAndHeight(tagText, document, ref num2, ref num3);
			num3 = num3 / 304.8 * 0.8;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			double result;
			if ((int)activeView.DetailLevel == 3)
			{
				result = hypiping.GetDiameter() / 2.0 + fontSetting.TubeDistance / 304.8 + 0.23120000000000002 * num3;
			}
			else
			{
				result = fontSetting.TubeDistance / 304.8 + 0.23120000000000002 * num3;
			}
			return result;
		}

		public double GetDistanceTagToLeader(Autodesk.Revit.DB.Document doc, string tagText)
		{
			double result = 0.0;
			Autodesk.Revit.DB.View activeView = doc.ActiveView;
			CommonFont fontSetting = new UserSetting().GetFontSetting();
			fontSetting.LeadDistance = this.GetTansferDistance(fontSetting.LeadDistance, 30);
			double num = 0.0;
			double num2 = 0.0;
			this.GetTagTextWidthAndHeight(tagText, doc, ref num, ref num2);
			num2 /= 304.8;
			double num3 = (double)doc.ActiveView.Scale / 100.0;
			if (RevitVersion.Version == "2015" || RevitVersion.Version == "2016" || RevitVersion.Version == "2017" || RevitVersion.Version == "2018")
			{
				result = fontSetting.LeadDistance / 304.8 * num3 + 0.23120000000000002 * num2;
			}
			else if (RevitVersion.Version == "2019")
			{
				result = fontSetting.LeadDistance / 304.8 * num3 + 0.408 * num2;
			}
			return result;
		}

		public double GetLeaderDistance(Autodesk.Revit.DB.Document doc)
		{
			Autodesk.Revit.DB.View activeView = doc.ActiveView;
			CommonFont fontSetting = new UserSetting().GetFontSetting();
			fontSetting.LeadDistance = this.GetTansferDistance(fontSetting.LeadDistance, 30);
			double num = (double)doc.ActiveView.Scale / 100.0;
			return fontSetting.LeadDistance / 304.8 * num;
		}

		public double GetCreatTagLength(string strText, Autodesk.Revit.DB.Document doc)
		{
			double num = 0.0;
			double num2 = 0.0;
			this.GetTagTextWidthAndHeight(strText, doc, ref num2, ref num);
			CommonFont fontSetting = new UserSetting().GetFontSetting();
			num2 = num2 * fontSetting.LeadRotia * 0.8;
			return num2;
		}

		private double GetTansferDistance(double oriDistance, int factor = 30)
		{
			return oriDistance * (double)factor;
		}

		private TextNoteType GetSpecialTextNodeType(Autodesk.Revit.DB.Document doc)
		{
			ICollection<Element> collection = new FilteredElementCollector(doc).OfClass(typeof(TextNoteType)).ToElements();
			TextNoteType textNoteType = null;
			foreach (Element element in collection)
			{
				if (element.Name.Equals("HY-仿宋-标注") || element.Name.Equals("HY-仿宋-标注"))
				{
					textNoteType = (element as TextNoteType);
				}
			}
			CommonFont fontSetting = new UserSetting().GetFontSetting();
			string text = "HY-仿宋-标注";
			if (textNoteType == null)
			{
				textNoteType = ((collection.First<Element>() as TextNoteType).Duplicate(text) as TextNoteType);
				textNoteType.get_Parameter(BuiltInParameter.TEXT_FONT).Set(fontSetting.Font);
				textNoteType.get_Parameter(BuiltInParameter.TEXT_SIZE).Set(fontSetting.FontHeight / 304.8);
				textNoteType.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).Set(fontSetting.FontGWRotia);
				textNoteType.get_Parameter(BuiltInParameter.TEXT_BACKGROUND).Set(1);
			}
			return textNoteType;
		}

		public void EditHYFamilyFont(Family family, CommonFont commonFont, bool beOnlyFont = false)
		{
			try
			{
				Document document = family.Document;
				Document document2 = family.Document.EditFamily(family);
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document2);
				filteredElementCollector.OfClass(typeof(TextElement));
				IEnumerator<Element> enumerator = filteredElementCollector.GetEnumerator();
				{
					enumerator.Reset();
					while (enumerator.MoveNext())
					{
						Element element = enumerator.Current;
						TextElementType symbol = (element as TextElement).Symbol;
						Parameter parameter = symbol.GetParameter(BuiltInParameter.TEXT_FONT);
						if (parameter != null)
						{
							parameter.Set(commonFont.Font);
						}
						if (!beOnlyFont)
						{
							parameter = symbol.GetParameter(BuiltInParameter.TEXT_SIZE);
							if (parameter != null)
							{
								parameter.Set(Convert.ToDouble(commonFont.FontHeight) / 304.8);
							}
							parameter = symbol.GetParameter(BuiltInParameter.TEXT_WIDTH_SCALE);
							if (parameter != null)
							{
								parameter.Set(Convert.ToDouble(commonFont.FontGWRotia));
							}
						}
					}
				}
				document2.LoadFamily(document, new HYFamilyLoadOptions());
			}
			catch (Exception)
			{
				throw;
			}
		}

		public void SetMarkAccuracy(Autodesk.Revit.DB.Document doc, string accuracyStr)
		{
			Units units = doc.GetUnits();
			FormatOptions formatOptions = units.GetFormatOptions(0);
			formatOptions.DisplayUnits = 0;
			double accuracy = accuracyStr.Equals("0.000") ? 0.001 : 0.01;
			formatOptions.Accuracy = accuracy;
			formatOptions.UnitSymbol = 0;
			formatOptions.SuppressTrailingZeros = false;
			formatOptions.SuppressLeadingZeros = false;
			formatOptions.UsePlusPrefix = false;
			formatOptions.UseDigitGrouping = false;
			formatOptions.SuppressSpaces = false;
			units.SetFormatOptions(0, formatOptions);
		}

		public void SetDiameterAccuracy(Autodesk.Revit.DB.Document doc)
		{
            Units units = doc.GetUnits();
            FormatOptions formatOptions = units.GetFormatOptions((UnitType)43);
            formatOptions.DisplayUnits = (DisplayUnitType)2;
            double accuracy = 0.01;
            formatOptions.Accuracy = accuracy;
            formatOptions.UnitSymbol = 0;
            formatOptions.SuppressTrailingZeros = true;
            formatOptions.SuppressLeadingZeros = false;
            formatOptions.UsePlusPrefix = false;
            formatOptions.UseDigitGrouping = false;
            formatOptions.SuppressSpaces = false;
            units.SetFormatOptions((UnitType)43, formatOptions);
            doc.SetUnits(units);
		}

		public void SetDuctDiameterUnit(Autodesk.Revit.DB.Document doc)
		{
			Units units = doc.GetUnits();
            FormatOptions formatOptions = units.GetFormatOptions((UnitType)16);
			string ductSuffix = new UserSetting().GetDuctMarkSetting().DuctSuffix;
            formatOptions.DisplayUnits = (DisplayUnitType)2;
			if (ductSuffix == "mm")
			{
                formatOptions.UnitSymbol = (UnitSymbolType)201;
			}
			else
			{
				formatOptions.UnitSymbol = 0;
			}
			formatOptions.SuppressTrailingZeros = true;
			formatOptions.SuppressLeadingZeros = false;
			formatOptions.UsePlusPrefix = false;
			formatOptions.UseDigitGrouping = false;
			formatOptions.SuppressSpaces = false;
            units.SetFormatOptions((UnitType)16, formatOptions);
			doc.SetUnits(units);
			doc.Regenerate();
		}

		public void AddAllShareParametes(UIApplication uiApp)
		{
			SubTransaction subTransaction = new SubTransaction(uiApp.ActiveUIDocument.Document);
			try
			{
				subTransaction.Start();
				string text = Path.Combine(Product.DataLocation, "MarkTagShareParam.txt");
				if (File.Exists(text))
				{
					string gropeName = "管道标记";
					List<string> listStr = null;
					CategorySet categories = null;
					this.GetCategorySetAndParameterNames(uiApp, ref categories, ref listStr, 0);
					YJKRevitTools.SetInstanceParameter(uiApp, text, gropeName, listStr, categories, false, BuiltInParameterGroup.PG_IDENTITY_DATA);
					this.GetCategorySetAndParameterNames(uiApp, ref categories, ref listStr, 1);
					YJKRevitTools.SetInstanceParameter(uiApp, text, gropeName, listStr, categories, false, BuiltInParameterGroup.PG_IDENTITY_DATA);
					this.GetCategorySetAndParameterNames(uiApp, ref categories, ref listStr, 2);
					YJKRevitTools.SetInstanceParameter(uiApp, text, gropeName, listStr, categories, false, BuiltInParameterGroup.PG_IDENTITY_DATA);
					this.GetCategorySetAndParameterNames(uiApp, ref categories, ref listStr, 3);
					YJKRevitTools.SetInstanceParameter(uiApp, text, gropeName, listStr, categories, false, BuiltInParameterGroup.PG_IDENTITY_DATA);
					this.GetCategorySetAndParameterNames(uiApp, ref categories, ref listStr, 4);
					YJKRevitTools.SetInstanceParameter(uiApp, text, gropeName, listStr, categories, false, BuiltInParameterGroup.PG_IDENTITY_DATA);
					subTransaction.Commit();
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				subTransaction.RollBack();
			}
		}

		private void GetCategorySetAndParameterNames(UIApplication uiApp, ref CategorySet categories, ref List<string> listParameterNames, int typeNum)
		{
			categories = uiApp.Application.Create.NewCategorySet();
			listParameterNames = new List<string>();
			switch (typeNum)
			{
			case 0:
			{
				Category category = uiApp.ActiveUIDocument.Document.Settings.Categories.get_Item(BuiltInCategory.OST_DuctCurves);
				categories.Insert(category);
				category = uiApp.ActiveUIDocument.Document.Settings.Categories.get_Item(BuiltInCategory.OST_PipeCurves);
				categories.Insert(category);
				category = uiApp.ActiveUIDocument.Document.Settings.Categories.get_Item(BuiltInCategory.OST_DuctTerminal);
				categories.Insert(category);
				listParameterNames.Add("分隔符");
				return;
			}
			case 1:
			{
				Category category = uiApp.ActiveUIDocument.Document.Settings.Categories.get_Item(BuiltInCategory.OST_DuctTerminal);
				categories.Insert(category);
				listParameterNames.Add("风口名称");
				listParameterNames.Add("风口数量");
				return;
			}
			case 2:
			{
				Category category = uiApp.ActiveUIDocument.Document.Settings.Categories.get_Item(BuiltInCategory.OST_PipeCurves);
				categories.Insert(category);
				listParameterNames.Add("直径前缀");
				listParameterNames.Add("壁厚");
				listParameterNames.Add("内径前缀");
				listParameterNames.Add("外径前缀");
				listParameterNames.Add("连字符");
				listParameterNames.Add("管道坡度");
				return;
			}
			case 3:
			{
				Category category = uiApp.ActiveUIDocument.Document.Settings.Categories.get_Item(BuiltInCategory.OST_DuctCurves);
				categories.Insert(category);
				listParameterNames.Add("顶绝对高程");
				listParameterNames.Add("底绝对高程");
				return;
			}
			case 4:
			{
				Category category = uiApp.ActiveUIDocument.Document.Settings.Categories.get_Item(BuiltInCategory.OST_DuctCurves);
				categories.Insert(category);
				category = uiApp.ActiveUIDocument.Document.Settings.Categories.get_Item(BuiltInCategory.OST_PipeCurves);
				categories.Insert(category);
				listParameterNames.Add("标高后缀");
				listParameterNames.Add("标高前缀");
				listParameterNames.Add("绝对标高");
				return;
			}
			default:
				return;
			}
		}

		public void AddShareParameters(UIApplication uiApp, ShareParamType shareType)
		{
			SubTransaction subTransaction = new SubTransaction(uiApp.ActiveUIDocument.Document);
			try
			{
				subTransaction.Start();
				string text = "E:\\BIMSpace\\Res\\Install\\HYRevit\\Data\\MarkTagShareParam.txt";
				string gropeName = "管道标记";
				List<string> listStr = null;
				BuiltInCategory builtInCategory = (Autodesk.Revit.DB.BuiltInCategory)(-1);
				switch (shareType)
				{
				case ShareParamType.Duct:
					listStr = this.GetDuctShareNames();
					builtInCategory = BuiltInCategory.OST_DuctCurves;
					break;
				case ShareParamType.Pipe:
					listStr = this.GetPipeShareNames();
					builtInCategory = BuiltInCategory.OST_PipeCurves;
					break;
				case ShareParamType.Terminal:
					listStr = this.GetTerminalShareNames();
					builtInCategory = BuiltInCategory.OST_DuctTerminal;
					break;
				}
				if (File.Exists(text))
				{
					YJKRevitTools.SetInstanceParameter(uiApp, text, gropeName, listStr, builtInCategory, false, BuiltInParameterGroup.PG_IDENTITY_DATA);
				}
				subTransaction.Commit();
			}
			catch (Exception ex)
			{
				ex.ToString();
				subTransaction.RollBack();
			}
		}

		private List<string> GetPipeShareNames()
		{
			return new List<string>
			{
				"直径前缀",
				"壁厚",
				"内径前缀",
				"外径前缀",
				"连字符",
				"标高后缀",
				"标高前缀",
				"管道坡度",
				"绝对标高"
			};
		}

		private List<string> GetDuctShareNames()
		{
			return new List<string>
			{
				"标高后缀",
				"标高前缀",
				"绝对标高"
			};
		}

		private List<string> GetTerminalShareNames()
		{
			return new List<string>
			{
				"风口名称",
				"风口数量"
			};
		}

		public YJKLine GetLineByDetailCurve(DetailCurve detailCurve)
		{
			XYZ hyendPoint = detailCurve.GeometryCurve.GetEndPoint(0);
			XYZ hyendPoint2 = detailCurve.GeometryCurve.GetEndPoint(1);
			return new YJKLine(hyendPoint, hyendPoint2);
		}

		public void SetAlignStyle(IndependentTag tag, YJKLine hyLine, XYZ basePoint, string tagType)
		{
			Document document = tag.Document;
			XYZ startPoint = hyLine.StartPoint;
			XYZ endPoint = hyLine.EndPoint;
			this.OrderLinePoints(ref startPoint, ref endPoint, basePoint);
			XYZ project = hyLine.GetProject(tag.TagHeadPosition);
			MarkOperation markOperation = new MarkOperation();
			double num = 0.0;
			double num2 = 0.0;
			string tagText = tag.TagText;
            //tagType == "水管";
			markOperation.GetTagTextWidthAndHeight(tagText, document, ref num, ref num2);
			num = this.GetWidthByRevitVersion(document, num);
			string pattern = "/";
			int count = Regex.Matches(tagText, pattern).Count;
			if (count <= 1)
			{
				num -= (double)(count * 60);
			}
			else
			{
				num -= (double)(Regex.Matches(tagText, pattern).Count * 120);
			}
			UserSetting userSetting = new UserSetting();
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			CommonFont fontSetting = userSetting.GetFontSetting();
			double num3 = -num / 609.6;
			string align = fontSetting.Align;
			XYZ xyz;
			if (!(align == "始端对齐"))
			{
				if (!(align == "中心对齐"))
				{
					if (!(align == "末端对齐"))
					{
						xyz = (startPoint.Add(endPoint) / 2.0).Subtract(project);
						num3 = 0.0;
					}
					else
					{
						xyz = endPoint.Subtract(project);
						num3 += this.GetDevision(tagType, "末端对齐") / 304.8 * (double)document.ActiveView.Scale / 100.0;
					}
				}
				else
				{
					xyz = (startPoint.Add(endPoint) / 2.0).Subtract(project);
					num3 = 0.0;
				}
			}
			else
			{
				xyz = startPoint.Subtract(project);
				num3 += this.GetDevision(tagType, "始端对齐") / 304.8 * (double)document.ActiveView.Scale / 100.0;
			}
			hyrotateFamilyInstance.MoveElement(xyz, tag);
			hyrotateFamilyInstance.MoveElement(xyz.Normalize() * num3, tag);
		}

		public void SetAlignStyleRiserPipe(IndependentTag tag, YJKLine hyLine, XYZ basePoint, string tagType)
		{
			Document document = tag.Document;
			XYZ startPoint = hyLine.StartPoint;
			XYZ endPoint = hyLine.EndPoint;
			this.OrderLinePoints(ref startPoint, ref endPoint, basePoint);
			XYZ project = hyLine.GetProject(tag.TagHeadPosition);
			MarkOperation markOperation = new MarkOperation();
			double num = 0.0;
			double num2 = 0.0;
			string tagText = tag.TagText;
            //tagType == "水管";
			markOperation.GetTagTextWidthAndHeight(tagText, document, ref num, ref num2);
			num = this.GetWidthByRevitVersion(document, num);
			string pattern = "/";
			int count = Regex.Matches(tagText, pattern).Count;
			if (count <= 1)
			{
				num -= (double)(count * 60);
			}
			else
			{
				num -= (double)(Regex.Matches(tagText, pattern).Count * 120);
			}
			UserSetting userSetting = new UserSetting();
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			CommonFont fontSetting = userSetting.GetFontSetting();
			double num3 = -num / 609.6;
			string align = fontSetting.Align;
			XYZ xyz;
			if (!(align == "始端对齐"))
			{
				if (!(align == "中心对齐"))
				{
					if (!(align == "末端对齐"))
					{
						xyz = (startPoint.Add(endPoint) / 2.0).Subtract(project);
						num3 = 0.0;
					}
					else
					{
						xyz = endPoint.Subtract(project);
						num3 += this.GetDevision(tagType, "末端对齐") / 304.8 * (double)document.ActiveView.Scale / 100.0;
					}
				}
				else
				{
					xyz = (startPoint.Add(endPoint) / 2.0).Subtract(project);
					num3 = 0.0;
				}
			}
			else
			{
				xyz = startPoint.Subtract(project);
				num3 += this.GetDevision(tagType, "始端对齐") / 304.8 * (double)document.ActiveView.Scale / 100.0;
			}
			hyrotateFamilyInstance.MoveElement(xyz, tag);
			hyrotateFamilyInstance.MoveElement(-xyz.Normalize() * 0.5, tag);
			hyrotateFamilyInstance.MoveElement(xyz.Normalize() * num3, tag);
		}

		public DimensionType GetDimensionTypeByName(Autodesk.Revit.DB.Document doc)
		{
			CommonFont fontSetting = new UserSetting().GetFontSetting();
			string text = string.Concat(new object[]
			{
				"HY-",
				fontSetting.Font,
				" ",
				fontSetting.FontHeight,
				"mm"
			});
			string copyBaseName = "对角线";
			List<Element> source = null;
			DimensionType dimensionType = null;
			if (this.SearchDimesionTypeByName(text, doc, ref source, ref dimensionType, copyBaseName) == null)
			{
				if (dimensionType != null)
				{
					dimensionType.Duplicate(text);
				}
				else
				{
					(source.First<Element>() as DimensionType).Duplicate(text);
				}
			}
			DimensionType dimensionType2 = this.SearchDimesionTypeByName(text, doc, ref source, ref dimensionType, string.Empty);
			doc.Regenerate();
			Parameter parameter = dimensionType2.GetParameter(BuiltInParameter.DIM_WITNS_LINE_CNTRL);
			if (parameter != null)
			{
				parameter.Set(1);
			}
			parameter = dimensionType2.GetParameter(BuiltInParameter.TEXT_FONT);
			if (parameter != null)
			{
				parameter.Set(fontSetting.Font);
			}
			parameter = dimensionType2.GetParameter(BuiltInParameter.TEXT_SIZE);
			if (parameter != null)
			{
				parameter.Set(fontSetting.FontHeight / 304.8);
			}
			parameter = dimensionType2.GetParameter(BuiltInParameter.DIM_TEXT_BACKGROUND);
			if (parameter != null)
			{
				parameter.Set(1);
			}
			parameter = dimensionType2.GetParameter(BuiltInParameter.TEXT_DIST_TO_LINE);
			if (parameter != null)
			{
				parameter.Set(fontSetting.LeadDistance / 304.8);
			}
			return dimensionType2;
		}

		private DimensionType SearchDimesionTypeByName(string dtName, Autodesk.Revit.DB.Document doc, ref List<Element> listElem, ref DimensionType baseType, string copyBaseName)
		{
			DimensionType result = null;
			IEnumerable<Element> enumerable = new FilteredElementCollector(doc).OfClass(typeof(DimensionType)).ToElements();
			baseType = null;
			foreach (Element element in enumerable)
			{
				if (element.Name == dtName)
				{
					result = (element as DimensionType);
					break;
				}
				if (!string.IsNullOrEmpty(copyBaseName) && element.Name.Contains(copyBaseName))
				{
					baseType = (element as DimensionType);
				}
			}
			return result;
		}

		public static string GetNewTagText(string tagText)
		{
			string result = tagText;
			int num = tagText.LastIndexOf(".");
			int num2 = tagText.IndexOf(".");
			if (num > 0 && num2 > 0)
			{
				if (num == num2)
				{
					string str = tagText.Substring(0, num2);
					string text = tagText.Substring(num2);
					text = MarkOperation.TrimEndZero(text);
					result = str + text;
				}
				else
				{
					string str2 = tagText.Substring(0, num2);
					string text2 = tagText.Substring(num2, num - num2);
					string text3 = tagText.Substring(num);
					int startIndex = 0;
					for (int i = 0; i < text2.Length; i++)
					{
						if (text2[i] == '0' && i + 1 < text2.Length && text2[i + 1] != '0')
						{
							startIndex = i + 1;
							break;
						}
					}
					text2 = text2.Substring(startIndex);
					text3 = MarkOperation.TrimEndZero(text3);
					result = str2 + text2 + text3;
				}
			}
			return result;
		}

		private static string TrimEndZero(string str)
		{
			string text = str;
			string str2 = string.Empty;
			if (!string.IsNullOrEmpty(str))
			{
				int num = -1;
				for (int i = str.Length - 1; i >= 0; i--)
				{
					if (int.TryParse(str[i].ToString(), out num) && i < str.Length - 1)
					{
						text = str.Substring(0, i + 1);
						str2 = str.Substring(i + 1);
						break;
					}
				}
				text = text.TrimEnd(new char[]
				{
					'0'
				});
				if (text == ".")
				{
					text = text.TrimEnd(new char[]
					{
						'.'
					});
				}
			}
			return text + str2;
		}

		private double GetDevision(string tagType, string align)
		{
			double result = 0.0;
			if (!(tagType == "风管"))
			{
				if (!(tagType == "水管"))
				{
					if (!(tagType == "水管标高"))
					{
						if (tagType == "风口")
						{
							if (!(align == "始端对齐"))
							{
								if (align == "末端对齐")
								{
									result = 170.0;
								}
							}
							else
							{
								result = 135.0;
							}
						}
					}
					else if (!(align == "始端对齐"))
					{
						if (align == "末端对齐")
						{
							result = 170.0;
						}
					}
					else
					{
						result = 130.0;
					}
				}
				else if (!(align == "始端对齐"))
				{
					if (align == "末端对齐")
					{
						result = 110.0;
					}
				}
				else
				{
					result = 50.0;
				}
			}
			else if (!(align == "始端对齐"))
			{
				if (align == "末端对齐")
				{
					result = 180.0;
				}
			}
			else
			{
				result = 150.0;
			}
			return result;
		}

		public double GetWidthByRevitVersion(Autodesk.Revit.DB.Document doc, double width)
		{
			if (RevitVersion.Version == "2016" || RevitVersion.Version == "2017" || RevitVersion.Version == "2018")
			{
				width += (double)(15000 / doc.ActiveView.Scale);
			}
			else if (RevitVersion.Version == "2015" || RevitVersion.Version == "2014")
			{
				width *= 95.0;
			}
			return width;
		}

		private void OrderLinePoints(ref XYZ startPoint, ref XYZ endPoint, XYZ basePoint)
		{
			double num = startPoint.DistanceTo(basePoint);
			double num2 = endPoint.DistanceTo(basePoint);
			if (num > num2)
			{
				XYZ xyz = startPoint;
				startPoint = endPoint;
				endPoint = xyz;
			}
		}
	}
}
